package cn.wolfcode.luowowo.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.IUserInfoStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.UserInfoStats;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
/**
 *  初始化热门数据监听器
 *  服务器启动时读取热门数据到redis中
 */
public class HotDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Value("${hotData.init}")
    //标记,决定是否进行热门数据读取
    private boolean flag;

    @Reference
    //引入攻略文章服务
    private IStrategyDetailService strategyDetailService;
    @Reference
    //引入用户服务
    private IUserInfoService userInfoService;
<<<<<<< HEAD
=======


>>>>>>> origin/zhiwei
    @Reference
    //引入攻略热门数据缓存服务
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    //引入用户数据缓存服务
    private IUserInfoStatsCacheService userInfoStatsCacheService;



    @Reference
    //引入用户数据缓存服务
    private IUserInfoStatsCacheService userInfoStatsCacheService;

    @Override
    //监听器的监听方法实现,创建应用时触发
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //标记为true时,执行g攻略文章数据初始化
        if (flag){
            System.out.println("开始初始化");
            //获取攻略文章列表
            List<StrategyDetail> details = strategyDetailService.listAll();
            for (StrategyDetail detail : details) {
                //判断此文章是否已经缓存在redis中
                if (strategyStatsCacheService.notExist(detail.getId())) {
                    //需要进行缓存
                    //攻略统计对象
                    StrategyStats stats = new StrategyStats();
                    //值迁移
                    stats.setDestId(detail.getDest().getId());
                    stats.setDestName(detail.getDest().getName());
                    stats.setTitle(detail.getTitle());
                    stats.setStrategyId(detail.getId());
                    stats.setViewnum(detail.getViewnum());
                    stats.setFavornum(detail.getFavornum());
                    stats.setSharenum(detail.getSharenum());
                    stats.setReplynum(detail.getReplynum());
                    stats.setThumbsupnum(detail.getThumbsupnum());
                    //保存到redis中
                    strategyStatsCacheService.save(detail.getId(),stats);
                }

                //排行榜数据初始化
                //飙升
                //传入飙升榜的枚举对象, 以及攻略文章的id
                //id用来获取攻略统计对象
                if (strategyStatsCacheService.notExistRank(RedisKey.STRATEGY_STATS_UP_SORT,detail.getId())){
                    //阅读数求和评论数
                    int num = detail.getViewnum() + detail.getReplynum();
                    //保存
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT,num,detail.getId());
                }
                //热门
                if (strategyStatsCacheService.notExistRank(RedisKey.STRATEGY_STATS_HOT_SORT,detail.getId())){
                    //求和
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    //保存
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT,num,detail.getId());

                }
            }

            //回答数排行榜 String---zset
            List<UserInfo> list = userInfoService.list();
            for (UserInfo userInfo : list) {
                if (userInfoStatsCacheService.notExist(userInfo.getId())) {
                    UserInfoStats stats = new UserInfoStats();
                    stats.setNickname(userInfo.getNickname());
                    stats.setLevel(userInfo.getLevel());
                    stats.setHeadImgUrl(userInfo.getHeadImgUrl());
                    stats.setUserInfoId(userInfo.getId());
                    stats.setAnswerNum(userInfo.getAnswerNum());
                    //保存到redis中
                    userInfoStatsCacheService.save(userInfo.getId(), stats);
                }

                //回答数排行榜数据初始化.
                if (userInfoStatsCacheService.notExistRank(RedisKey.ANSWER_ZSET_SORT, userInfo.getId())) {
                    Integer num = userInfo.getAnswerNum();
                    //保存
                    userInfoStatsCacheService.addRank(RedisKey.ANSWER_ZSET_SORT, num, userInfo.getId());
                }
            }

            System.out.println("初始化完成");
        }
    }


}
